// d:\lingfe\xcx\bzhiDome\pages\info\info.js
// var Bmob = require('../../../utils/title.js');
//获取应用实例
var app = getApp()

Page({
  data: {
    infoDataList:[],
  },

  //页面加载
  onLoad: function (options) {
    var that=this;
    // tt.setNavigationBarTitle({title: infoData.mahjongTitle});
    var infoData = tt.getStorageSync("infoData");
    that.setData({
      publicModel: false,
      infoDataList: tt.getStorageSync("infoDataList"),
      infoData:this.getinfoData(infoData),
      index: tt.getStorageSync("index")
    });

    //插屏广告
    app.interstitialAd();
  },

  //下一题
  next(){
    var that=this;
    var index = that.data.index;
    index ++ ;
    var infoData = that.data.infoDataList[index];
    that.setData({
      showModal:false,
      publicModel:false,
      index:index,
      infoData:this.getinfoData(infoData)
    });
  },

  //处理图片路径
  getinfoData(infoData){
    infoData.mahjongCombination = this.showImg(infoData.mahjongCombination);
    infoData.mahjongCover = this.showImg(infoData.mahjongCover);
    infoData.mahjongAnswerAAA = this.showImg(infoData.mahjongAnswerAAA);
    infoData.mahjongAnswerBBB = this.showImg(infoData.mahjongAnswerBBB);
    infoData.mahjongAnswerCCC = this.showImg(infoData.mahjongAnswerCCC);
    infoData.mahjongAnswerDDD = this.showImg(infoData.mahjongAnswerDDD);
    return infoData;
  },

  //返回首页
  fanhuiTab:function(){
    tt.navigateBack();
  },

  //预览
  showImg:function(imgUrl){
    var that = this;
    console.info("imgurl = ",imgUrl);
    if(imgUrl.indexOf("http") !=-1){
      return imgUrl;
    }else{
      return app.globalData.domainName+imgUrl;
    }
  },

  /** 显示-广告弹窗按钮 */
  showTip:function (e) {
    this.setData({
      showModal:true
    });
  },

  /** 关闭-广告弹窗按钮 */
  preventHide:function(e){
    var that=this;
    //tt.showToast({ title: "求支持!",icon:"fail",duration:2000});
    if(e.currentTarget.id == 1){
      //选择
      tt.showModal({ title:"重要提示",content: "确定关闭吗?",
        success:function(res){
          if(res.cancel){
          }else{
            that.setData({publicModel:false});
          }
        }
      });
    }else{
      that.setData({showModal:false});
    }
  },

  /** 观看广告获取答案 */
  getAnswerBtndtap:function(){  
    var that=this;
    let videoAd = app.globalData.videoAd;
    that.setData({showModal:false});

    // 照抄的官方例子
    videoAd
    .show()
    .then(() => {
      console.log("广告显示成功");
    })
    .catch((err) => {
      console.log("广告组件出现问题", err);
      tt.showToast({ 
        title:"检测到特殊用户!直接给予奖励！",
        icon:"none",
        duration:3000
      });

      // 可以手动加载一次
      videoAd.load().then(() => {
        console.log("手动加载成功");
        // 加载成功后需要再显示广告
        return videoAd.show();
      });
    });

    //广告
    if (videoAd) {
      // 广告关闭
      videoAd.onClose(res => {
        if (res.isEnded) {
          console.log('广告关闭', res)
          //给予奖励
          that.setData({publicModel:true});
        } else {
          tt.showToast({ title:"没有看完广告无法获得奖励！",icon:"none",duration:3000 });
        }
      })
      
      //加载错误
      videoAd.onError((err) => {
        console.log('加载异常', err)
        tt.showToast({ title:"广告调用失败！直接给予奖励！",icon:"none",duration:3000 });
        //给予奖励
        that.setData({publicModel:true});
      });
    }
  },

})